<?php

namespace Home\Controller;

class OrderController extends BaseController {

    public function _initialize() {
        parent::_initialize();

        if (empty($this->user_id)) {
            $this->error("请登录!", '', -1);
        }
    }

    public function getlist() {
        $status = I('status', -1);

        $where['o.user_id'] = $this->user_id;
//        $where['g.is_del'] = 0;

        if ($status != -1) {
            $where['g.status'] = $status;
        }

        $orderList = M()
                ->table('ot_order o')
                ->join('left join ot_goods g on o.goods_id = g.goods_id')
                ->field('o.order_id,o.order_sn,o.status,o.order_amount,o.pay_time,g.goods_id,g.goods_name,g.head_img')
                ->where($where)
                ->select();

        $this->success('', $orderList);
    }

    public function orderInfo() {
        $order_id = I('order_id', 0, 'intval');

        $where['o.user_id'] = $this->user_id;

        if ($order_id != 0) {
            $where['o.order_id'] = $order_id;
        } else {
            $this->error('错误的订单');
        }

        $orderList = M()
                ->table('ot_order o')
                ->join('left join ot_goods g on o.goods_id = g.goods_id')
                ->field('o.*,g.goods_id,g.goods_name,g.head_img')
                ->where($where)
                ->find();

        $this->success('', $orderList);
    }

    public function setOrderStatus() {
        $status = I('status', -1);
        $order_id = I('order_id', 0, 'intval');
        $where['o.user_id'] = $this->user_id;

        if ($status == -1) {
            $this->error('请确认更新的状态');
        }

        if ($order_id == 0) {
            $this->error('错误的订单');
        }

        $where['o.order_id'] = $order_id;

        $data['status'] = $status;

        $res = M("Gateway")->where($where)->save($data);
        if ($res) {
            $this->success();
        } else {
            $this->error('修改失败');
        }
    }

    public function add() {
        $goods_id = I('goods_id', 0, 'intval');
        $data['goods_num'] = I('goods_num', 1, 'intval');
        $data['type'] = I('type', 0, 'intval');
        $data['is_install'] = I('is_install', 0, 'intval');
        $data['province'] = I('province');
        $data['city'] = I('city');
        $data['county'] = I('county');
        $data['address'] = I('address');
        $data['consignee'] = I('consignee');
        $data['phone'] = I('phone');
        $data['install_fee'] = I('install_fee', 0);
        $data['discount_amount'] = I('discount_amount', 0);
        $data['pay_type'] = I('pay_type', 0, 'intval');
        $data['remark'] = I('remark');

        $goodsWhere['is_del'] = 0;
        $goodsWhere['status'] = 0;

        if ($goods_id != 0) {
            $goodsWhere['goods_id'] = $goods_id;
        }

        $goods_info = M('Goods')->where($goodsWhere)->find();

        if (empty($goods_info)) {
            $this->error('错误的商品信息');
            return;
        }

        $data['goods_id'] = $goods_info['goods_id'];
        if ($data['type'] == 0) {
            $data['goods_price'] = $goods_info['price'];
        } else {
            $data['goods_price'] = $goods_info['trial_price'];
        }

        $data['order_amount'] = $data['goods_price'] * $data['goods_num'] - $data['discount_amount'];

        $res = M('Order')->add($data);

        if ($res) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }

}
